
#mysql
import pandas as pd
from sqlalchemy import create_engine

# 1. 读取CSV文件
csv_file = 'date/京东-麻辣王子辣条评论cled.csv'
df = pd.read_csv(csv_file)
# 2. 连接到MySQL数据库
db_user = 'root'
db_pass = '123456'
db_name = 'big_work'
db_host = 'localhost'
# 创建数据库连接
engine = create_engine(f'mysql+pymysql://{db_user}:{db_pass}@{db_host}/{db_name}')
# 3. 将数据插入到MySQL中
# 假设你的表名为'your_table_name'，并且它的列与DataFrame的列匹配
table_name = 'comments_table'
if_exists = 'replace'  # 如果表已存在，则替换它（也可以选择'append', 'fail'）
df.to_sql(name=table_name, con=engine, if_exists=if_exists, index=False)
print(f"Data from {csv_file} has been successfully loaded to {table_name} in MySQL.")